import { ApiCall } from "tsrpc";
import { ReqCreateRoom, ResCreateRoom } from "../../../tsrpc/shared/protocols/hall/PtlCreateRoom";
import { hs } from "../../../HallServiceEntra";

export default async function (call: ApiCall<ReqCreateRoom, ResCreateRoom>) {
    // TODO
    if (hs.HallModel.roomServiceList.length == 0) {
        call.error("没有房间服务器", {code:"NOT_ROOM_SERVICE"})
        return;
    }
    const index = Math.floor(Math.random()*hs.HallModel.roomServiceList.length);
    const roomService = hs.HallModel.roomServiceList[index];
    const ret = await roomService.wsc.callApi("CreateRoom", {
        roomName: call.req.roomName,
        password: call.req.password
    })
    if (ret.isSucc) {
        call.succ({
            roomId: ret.res.roomId
        })
        return;
    }
    call.error("创建房间失败", {code:"CREATE_ROOM_FAIL"});

}